/*
 * Copyright 2009 Nordic Consulting & Development Company
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package pl.ncdc.web.login.client;

import pl.ncdc.web.login.client.service.AuthenticationResult;
import pl.ncdc.web.login.client.service.AuthenticationService;
import pl.ncdc.web.login.client.service.AuthenticationServiceAsync;
import pl.ncdc.web.login.client.service.UserService;
import pl.ncdc.web.login.client.service.UserServiceAsync;
import pl.ncdc.web.login.client.ui.AuthenticationListener;
import pl.ncdc.web.login.client.ui.LoginWidget;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;

/**
 * Demo showing <code>ncdc-web-login</code> functionality.
 * <p>
 * Created on Jan 28, 2009
 *
 * @author hshsce
 * @version $Id: Demo.java 206 2009-06-04 10:03:16Z hshsce $
 */
public class Demo implements EntryPoint {

	private UserServiceAsync m_userService;

	private AuthenticationServiceAsync m_authService;

	private DeckPanel m_panel;

	/** {@inheritDoc} */
	public void onModuleLoad() {
		m_userService = GWT.create(UserService.class);
		m_authService = GWT.create(AuthenticationService.class);
		final HTML text = HTML.wrap(RootPanel.get("text").getElement());

		m_panel = new DeckPanel();
		m_panel.setAnimationEnabled(true);
		m_panel.add(newLoginPanel());
		m_panel.add(newFakeApp(text));
		m_panel.showWidget(0);

		RootPanel.get("app").add(m_panel);
	}

	private Widget newLoginPanel() {
		final SimplePanel panel = new SimplePanel();
		panel.setStyleName("ncdc-web-login-demo-LoginPanel");
		DOM.setStyleAttribute(panel.getElement(), "textAlign", "center"); //horizontal centering for IE
		panel.setWidget(newLoginDecorator());
		return panel;
	}

	private Widget newLoginDecorator() {
		final DecoratorPanel panel = new DecoratorPanel();
		DOM.setStyleAttribute(panel.getElement(), "margin", "0 auto"); //horizontal centering for rest of the world
		panel.setWidget(newLoginWidget());
		return panel;
	}

	private Widget newLoginWidget() {
		final LoginWidget widget = new LoginWidget("Welcome", "images/users", "jpg", m_authService, m_userService);
		widget.addAuthenticationListener(new AuthenticationListener() {
			public void onAuthenticationResponse(final String uid, final AuthenticationResult result) {
				if (result == AuthenticationResult.OK) {
					m_panel.showWidget(1);
				}
			}
		});
		return widget;
	}

	private Widget newFakeApp(final HTML text) {
		final HorizontalPanel panel = new HorizontalPanel();
		panel.add(newTreeMenu());
		panel.add(text);
		return panel;
	}

	private Widget newTreeMenu() {
		final Tree tree = new Tree();
		tree.setAnimationEnabled(true);
		tree.addItem("about");
		tree.addItem("account");
		tree.addItem("settings");
		tree.addItem("documents");
		tree.addItem("logout");
		tree.addSelectionHandler(new SelectionHandler<TreeItem>() {
			public void onSelection(final SelectionEvent<TreeItem> event) {
				if (event.getSelectedItem().getText().equals("logout")) {
					logout();
				}
			}
		});
		return tree;
	}

	private void logout() {
		m_authService.logout(new AsyncCallback<Void>() {
			public void onFailure(final Throwable caught) {
				Window.alert("Cannot logout from server");

			}
			public void onSuccess(final Void result) {
				m_panel.showWidget(0);
			}
		});
	}

}
